import leftopic from '@/components/leftopic'
import loading from '@/components/loading'
export default {
  data(){
    return{
      items: { status: true, list: [] }
    }
  },
  components:{
    leftopic,
    loading
  },
  methods: {
    gotofood_child:function(shopId){
      this.getShopById(shopId);
    },
    getshopstartmes:function(){
      var _this = this;
      function setItem(status, list) {
        _this.items.status = status;
        _this.items.list = list;
      }
      function compare(property, x) {
        return function(obj1, obj2) {
            var value1 = obj1[property];
            var value2 = obj2[property];
            return (value1 - value2) * x; //1:升序 //-1:降序
        }
      }

      this.$http.post(
        "/api/selectshop",
        {
          // data //
      })
      .then( function(res) {
        console.log(res.data.status+" 推荐店铺列表-请求成功");
        var le=res.data.data.length;
        var aShop = [];
        for(let i=0;i<le;i++){
          aShop[i] = {
            topic: res.data.data[i].name,
            stars: res.data.data[i].grade,
            id:    res.data.data[i].id
          }
        }
        aShop.sort(compare('stars', -1));
        setItem(true, aShop);
      })
      .catch( function(err) {
        console.log("服务器错误");
        console.log(err);
        setItem(false, ["服务器错误",err]);
      });
    },
    getShopById: function(shopId) {
      let _this = this;
      this.$http({
        method: "post",
        url: "/api/selectshopbyid",
        data: {
          id: shopId
        }
      })
      .then( function(res) {
        //console.log(res.data.data[0])
        let oShop = res.data.data[0];
        _this.$router.replace({path: '/home/foodrecom_child', query: oShop });
      })
      .catch( function(err) {
        console.log("服务器错误", err);
      });
    }
  },
  mounted:function () {
    this.getshopstartmes();
  }
}
